package com.atguigu.spzx.manager.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.UUID;
import com.atguigu.spzx.common.exception.GuiguException;
import com.atguigu.spzx.manager.properties.MinioProperties;
import com.atguigu.spzx.manager.service.FileUploadService;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.errors.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;

@Service
public class FileUploadServiceImpl implements FileUploadService {
    @Autowired
    private MinioProperties minioProperties;
    @Override
    public String fileUpload(MultipartFile multipartFile) {
        // 创建一个Minio的客户端对象
        MinioClient minioClient=MinioClient.builder().endpoint(minioProperties.getEndpointUrl())
                .credentials(minioProperties.getAccessKey(),minioProperties.getSecreKey()).build();
      try {
          // 判断桶是否存在
          boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(minioProperties.getBucketName()).build());
          if (!found) {       // 如果不存在，那么，此时就创建一个新的桶
              minioClient.makeBucket(MakeBucketArgs.builder().bucket(minioProperties.getBucketName()).build());
          } else {  // 如果存在打印信息
              System.out.println("Bucket 'spzx-bucket' already exists.");
          }
          // 设置存储对象名称
          String dateDir = DateUtil.format(new Date(), "yyyyMMdd");
          String uuid = UUID.randomUUID().toString().replace("-", "");
          //20230801/443e1e772bef482c95be28704bec58a901.jpg
          String fileName = dateDir+"/"+uuid+multipartFile.getOriginalFilename();
          System.out.println(fileName);

          PutObjectArgs putObjectArgs = PutObjectArgs.builder()
                  .bucket(minioProperties.getBucketName())
                  .stream(multipartFile.getInputStream(), multipartFile.getSize(), -1)
                  .object(fileName)
                  .build();
          minioClient.putObject(putObjectArgs) ;
          return minioProperties.getEndpointUrl() + "/" + minioProperties.getBucketName() + "/" + fileName ;
      }catch (Exception e){
          throw new RuntimeException(e);
      }



        //获得上传的路径返回



    }
}
